package com.junko.service.back.impl;

import com.junko.core.base.BaseServiceImpl;
import com.junko.dao.back.ModuleDao;
import com.junko.dao.back.OperatorDao;
import com.junko.dao.back.SystemDao;
import com.junko.model.back.Module;
import com.junko.model.back.Operator;
import com.junko.model.back.System;
import com.junko.model.back.ZtreeNode;
import com.junko.service.back.IOperatorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class OperatorService extends BaseServiceImpl<Operator> implements IOperatorService {
    @Autowired
    private OperatorDao operatorDao;
    @Autowired
    private ModuleDao moduleDao;
    @Autowired
    private SystemDao systemDao;

    @Override
    public List<Operator> findByModuleId(Integer moduleId) {
        return operatorDao.findByModuleId(moduleId);
    }


    /**
     * 获取子节点数据
     * @param moduleId
     * @return
     */
    @Override
    public List<ZtreeNode> treeData(Integer moduleId){
        List<Module> listModule = new ArrayList<>();
        List<Operator> operatorList = new ArrayList<>();

        if (null == moduleId) {
            List<System> systems = systemDao.findAll();
            for (System system : systems) {
                listModule.addAll(moduleDao.findTopParentsBySystemId(system.getId()));
            }
//            listModule = moduleDao.listRootModules();
        } else {
            moduleId = Integer.parseInt((moduleId+"").replace("module_", ""));
            listModule = moduleDao.findByParentId(moduleId);
            operatorList = operatorDao.findByModuleId(moduleId);
        }
        List<ZtreeNode> nodeList = new ArrayList<>();
        ZtreeNode node;
        for (Module module : listModule) {
            node = new ZtreeNode();
            node.setId("module_" + module.getId());
            node.setName(module.getName());
            node.setIsParent(true);
            node.setNocheck(true);
            nodeList.add(node);
        }
        for (Operator operator : operatorList) {
            node = new ZtreeNode();
            node.setId("operator_" + operator.getId());
            node.setName(operator.getName());
            node.setIsParent(false);
            node.setChecked(false);
            nodeList.add(node);
        }
        return nodeList;
    }

    /**
     * 获取节点数据
     * @return
     */
    @Override
    public List<ZtreeNode> tree(){
        List<Module> rootModuleList = new ArrayList<>();
        List<System> systems = systemDao.findAll();
        for (System system : systems) {
            rootModuleList.addAll(moduleDao.findTopParentsBySystemId(system.getId()));
        }
        List<ZtreeNode> nodeList = new ArrayList<>();
        for (Module module : rootModuleList) {
            ZtreeNode node = new ZtreeNode();
            node.setId(module.getId()+"");
            node.setName(module.getName());
            node.setIsParent(true);
            node.setNocheck(true);
            recursion(node, module);
            nodeList.add(node);
        }

        return nodeList;
    }

    @Override
    public Operator findByUrl(String url) {
        return operatorDao.findByUrl(url);
    }

    /**
     * 递归获取节点信息
     * @param treeNode
     * @param pModule
     */
    private void recursion(ZtreeNode treeNode, Module pModule){
        List<ZtreeNode> children = new ArrayList<ZtreeNode>();
        // 功能
        List<Operator> operatorList = operatorDao.findByModuleId(pModule.getId());
        for (Operator operator : operatorList) {
            ZtreeNode node = new ZtreeNode();
            node.setId(operator.getId());
            node.setName(operator.getName());
            node.setIsParent(false);
            node.setChecked(false);
            children.add(node);
        }
        // 模块
        List<Module> moduleList = moduleDao.findByParentId(pModule.getId());
        for (Module module : moduleList) {
            ZtreeNode node = new ZtreeNode();
            node.setId(module.getId()+"");
            node.setName(module.getName());
            node.setIsParent(true);
            node.setNocheck(true);
            recursion(node, module);
            children.add(node);
        }
        treeNode.setChildren(children);
    }
}